package com.ybl.store.backend.controller.deal;

import com.alibaba.excel.EasyExcel;
import com.ybl.store.backend.dto.req.deal.DealVoucherSaveReqDTO;
import com.ybl.store.backend.dto.req.deal.DealVoucherUpdateReqDTO;
import com.ybl.store.backend.dto.resp.deal.DealVoucherExportRespDTO;
import com.ybl.store.backend.dto.resp.deal.DealVoucherPageRespDTO;
import com.ybl.store.backend.service.deal.DealVoucherService;
import com.ybl.store.common.convention.errorcode.BaseErrorCode;
import com.ybl.store.common.convention.exception.ClientException;
import com.ybl.store.common.convention.result.Result;
import com.ybl.store.common.convention.result.Results;
import com.ybl.store.common.entity.ListRespDTO;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/backend/deal/voucher")
public class DealVoucherController {

    private final DealVoucherService dealVoucherService;

    @PostMapping
    public Result<Void> saveDealVoucher(@RequestBody DealVoucherSaveReqDTO requestParam){
        dealVoucherService.saveDealVoucher(requestParam);
        return Results.success();
    }

    @DeleteMapping
    public Result<Void> deleteDealVoucher(@RequestParam Long id) {
        dealVoucherService.deleteDealVoucher(id);
        return Results.success();
    }

    @PutMapping
    public Result<Void> updateDealVoucher(@RequestBody DealVoucherUpdateReqDTO requestParam){
        dealVoucherService.updateDealVoucher(requestParam);
        return Results.success();
    }

    @GetMapping("/page")
    public Result<ListRespDTO<DealVoucherPageRespDTO>> pageDealVoucher(
            @RequestParam Integer pn,
            @RequestParam Integer ps,
            @RequestParam(required = false) Long storeId,
            @RequestParam(required = false) Long clerkId,
            @RequestParam(required = false) Long customerId,
            @RequestParam(required = false) Long voucherId,
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime,
            @RequestParam(required = false) Integer type)
    {
        return Results.success(dealVoucherService.pageDealVoucher(pn, ps, storeId, clerkId, customerId, voucherId, startTime, endTime, type));
    }

    @GetMapping("/export")
    public void export(HttpServletResponse response) {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment; filename=data.xlsx");

        try {
            List<DealVoucherExportRespDTO> export = dealVoucherService.export();
            EasyExcel.write(response.getOutputStream(), DealVoucherExportRespDTO.class)
                    .sheet("data")
                    .doWrite(export);
        }
        catch (IOException e) {
            throw new ClientException(BaseErrorCode.EXCEL_ERROR);
        }
    }
}
